home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / CLASSSRC.PAK / PROFILE.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  2KB  |  100 lines

  1. //----------------------------------------------------------------------------
  2. // Borland WinSys Library
  3. // Copyright (c) 1991, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   5.4  $
  6. //
  7. // Implementation of TProfile class
  8. //----------------------------------------------------------------------------
  9. #include <winsys/pch.h>
  10. #include <winsys/profile.h>
  11. #include <services/memory.h>
  12.  
  13. //
  14. // Use system profile for filename==0. Use system to search in normal places
  15. // if no path is included in filename.
  16. //
  17. TProfile::TProfile(const _TCHAR far* section, const _TCHAR far* filename)
  18. {
  19.   Section  = section ? strnewdup(section) : 0;
  20.  
  21.   // Use OpenFile to track down the given filename
  22.   //   if can't find it, use copy of original name,
  23.   //   if found, use copy of full path
  24.   //
  25.   if (filename) {
  26.     OFSTRUCT ofs;
  27.     ofs.cBytes = sizeof ofs;
  28.     FileName = strnewdup(
  29.       (OpenFile(filename, &ofs, OF_EXIST) == HFILE_ERROR) ? filename : ofs.szPathName
  30.     );
  31.   }
  32.   else {
  33.     FileName = 0;
  34.   }
  35. }
  36.  
  37. //
  38. // Clean up buffers
  39. //
  40. TProfile::~TProfile()
  41. {
  42.   delete[] FileName;
  43.   delete[] Section;
  44. }
  45.  
  46. //
  47. // Retrieve an integer value from the profile
  48. //
  49. int
  50. TProfile::GetInt(const _TCHAR far* key, int defaultInt)
  51. {
  52.   return FileName
  53.     ? ::GetPrivateProfileInt(Section, key, defaultInt, FileName)
  54.     : ::GetProfileInt(Section, key, defaultInt);
  55. }
  56.  
  57. //
  58. // Return all section values if key==0
  59. //
  60. bool
  61. TProfile::GetString(const _TCHAR far* key, _TCHAR far* buff, unsigned buffSize,
  62.                     const _TCHAR far* defaultString)
  63. {
  64.   return FileName
  65.     ? ::GetPrivateProfileString(Section, key, defaultString, buff, buffSize, FileName)
  66.     : ::GetProfileString(Section, key, defaultString, buff, buffSize);
  67. }
  68.  
  69. //
  70. // Write an integer value to the profile
  71. //
  72. bool
  73. TProfile::WriteInt(const _TCHAR far* key, int value)
  74. {
  75.   _TCHAR buf[16];
  76.   itoa(value, buf, 10);
  77.   return WriteString(key, buf);
  78. }
  79.  
  80. //
  81. // Write an string value to the profile
  82. //
  83. bool
  84. TProfile::WriteString(const _TCHAR far* key, const _TCHAR far* str)
  85. {
  86.   return FileName
  87.     ? ::WritePrivateProfileString(Section, key, str, FileName)
  88.     : ::WriteProfileString(Section, key, str);
  89. }
  90.  
  91. //
  92. // Make sure that all written profile values are flushed to the actual file
  93. //
  94. void
  95. TProfile::Flush()
  96. {
  97.   if (FileName)
  98.     ::WritePrivateProfileString(0, 0, 0, FileName);
  99. }
  100.